export const timeDiffTrans = (beginTime, endTime) => {
  var timeDiff = "";
  var date =
    new Date(Date.parse(endTime.replace(/-/g, "/"))).getTime() -
    new Date(Date.parse(beginTime.replace(/-/g, "/"))).getTime();
  //计算出相差天数
  var days = Math.floor(date / (24 * 3600 * 1000));
  if (days > 0) {
    timeDiff += days + "天";
  }
  //计算出小时数
  var leave1 = date % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  if (hours > 0) {
    timeDiff += hours + "小时";
  }
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));
  if (minutes > 0) {
    timeDiff += minutes + "分钟";
  }
  //计算相差秒数
  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  if (seconds >= 0) {
    timeDiff += seconds + "秒";
  }
  return timeDiff;
};
export const timeMixin = {
  data() {
    return {
      timer: null, // 计时器
      times: "<span>0</span>天<span>00</span>时<span>0</span>分", // 倒计时到分钟，比真正的秒还差60秒，需要赋值的时候加进去，如果是到秒，就不需要了
      countDown: 0
    };
  },
  methods: {
    timeDown() {
      // 倒计时
      clearInterval(this.timer);
      var nowDate = Math.round(new Date() / 1000); // 当前时间
      //console.log(nowDate, "nowDate");
      var endtime = this.countDown;
      if (endtime < nowDate) {
        return;
      }
      var totalSeconds = parseInt(endtime - nowDate); // 相差的总秒数
      //console.log(totalSeconds);
      //天数
      var days = Math.floor(totalSeconds / (60 * 60 * 24));
      //取模（余数）
      var modulo = totalSeconds % (60 * 60 * 24);
      //小时数
      var hours = Math.floor(modulo / (60 * 60));
      hours = hours < 10 ? "0" + hours : hours;
      modulo = modulo % (60 * 60);
      //分钟
      var minutes = Math.floor(modulo / 60);
      minutes = minutes < 10 ? "0" + minutes : minutes;
      //秒
      var seconds = modulo % 60;
      //输出到页面
      this.times = `<span>${days}</span>天 <span>${hours}</span>时 <span>${minutes}</span>分 <span>${seconds}</span>秒`;
      //console.log(days + "天" + hours + "时" + minutes + "分");
      if (totalSeconds <= 60) {
        clearInterval(this.timer);
      } else {
        this.timer = setInterval(this.timeDown, 1000); //倒计时循环
      }
    }
  }
};
